home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / test / feedtest.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  13.3 KB  |  220 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import unittest
  5. from tempfile import mkstemp
  6. from time import sleep
  7. import config
  8. import prefs
  9. import dialogs
  10. import database
  11. from feed import validateFeedURL, normalizeFeedURL, Feed
  12. from test.framework import DemocracyTestCase, EventLoopTest
  13.  
  14. class FakeDownloader:
  15.     pass
  16.  
  17.  
  18. class AcceptScrapeTestDelegate:
  19.     
  20.     def __init__(self):
  21.         self.calls = 0
  22.  
  23.     
  24.     def runDialog(self, dialog):
  25.         self.calls += 1
  26.         if not isinstance(dialog, dialogs.ChoiceDialog):
  27.             raise AssertionError('Only expected ChoiceDialogs')
  28.         
  29.         if not dialog.title.startswith('Channel is not compatible'):
  30.             raise AssertionError('Only expected scrape dialogs')
  31.         
  32.         dialog.choice = dialogs.BUTTON_YES
  33.         dialog.callback(dialog)
  34.  
  35.  
  36.  
  37. class FeedURLValidationTest(DemocracyTestCase):
  38.     
  39.     def test(self):
  40.         self.assertEqual(validateFeedURL(u'http://foo.bar.com/'), True)
  41.         self.assertEqual(validateFeedURL(u'https://foo.bar.com/'), True)
  42.         self.assertEqual(validateFeedURL(u'feed://foo.bar.com/'), False)
  43.         self.assertEqual(validateFeedURL(u'http://foo.bar.com'), False)
  44.         self.assertEqual(validateFeedURL(u'http:foo.bar.com/'), False)
  45.         self.assertEqual(validateFeedURL(u'https:foo.bar.com/'), False)
  46.         self.assertEqual(validateFeedURL(u'feed:foo.bar.com/'), False)
  47.         self.assertEqual(validateFeedURL(u'http:/foo.bar.com/'), False)
  48.         self.assertEqual(validateFeedURL(u'https:/foo.bar.com/'), False)
  49.         self.assertEqual(validateFeedURL(u'feed:/foo.bar.com/'), False)
  50.         self.assertEqual(validateFeedURL(u'http:///foo.bar.com/'), False)
  51.         self.assertEqual(validateFeedURL(u'https:///foo.bar.com/'), False)
  52.         self.assertEqual(validateFeedURL(u'feed:///foo.bar.com/'), False)
  53.         self.assertEqual(validateFeedURL(u'foo.bar.com'), False)
  54.         self.assertEqual(validateFeedURL(u'crap:foo.bar.com'), False)
  55.         self.assertEqual(validateFeedURL(u'crap:/foo.bar.com'), False)
  56.         self.assertEqual(validateFeedURL(u'crap://foo.bar.com'), False)
  57.         self.assertEqual(validateFeedURL(u'crap:///foo.bar.com'), False)
  58.  
  59.  
  60.  
  61. class FeedURLNormalizationTest(DemocracyTestCase):
  62.     
  63.     def test(self):
  64.         self.assertEqual(normalizeFeedURL(u'http://foo.bar.com'), u'http://foo.bar.com/')
  65.         self.assertEqual(normalizeFeedURL(u'https://foo.bar.com'), u'https://foo.bar.com/')
  66.         self.assertEqual(normalizeFeedURL(u'feed://foo.bar.com'), u'http://foo.bar.com/')
  67.         self.assertEqual(normalizeFeedURL(u'http:foo.bar.com'), u'http://foo.bar.com/')
  68.         self.assertEqual(normalizeFeedURL(u'https:foo.bar.com'), u'https://foo.bar.com/')
  69.         self.assertEqual(normalizeFeedURL(u'feed:foo.bar.com'), u'http://foo.bar.com/')
  70.         self.assertEqual(normalizeFeedURL(u'http:/foo.bar.com'), u'http://foo.bar.com/')
  71.         self.assertEqual(normalizeFeedURL(u'https:/foo.bar.com'), u'https://foo.bar.com/')
  72.         self.assertEqual(normalizeFeedURL(u'feed:/foo.bar.com'), u'http://foo.bar.com/')
  73.         self.assertEqual(normalizeFeedURL(u'http:///foo.bar.com'), u'http://foo.bar.com/')
  74.         self.assertEqual(normalizeFeedURL(u'https:///foo.bar.com'), u'https://foo.bar.com/')
  75.         self.assertEqual(normalizeFeedURL(u'feed:///foo.bar.com'), u'http://foo.bar.com/')
  76.         self.assertEqual(normalizeFeedURL(u'foo.bar.com'), u'http://foo.bar.com/')
  77.  
  78.  
  79.  
  80. class FeedTestCase(EventLoopTest):
  81.     
  82.     def setUp(self):
  83.         EventLoopTest.setUp(self)
  84.         self.everything = database.defaultDatabase
  85.         (handle, self.filename) = mkstemp('.xml')
  86.  
  87.     
  88.     def writefile(self, content):
  89.         self.url = u'file://%s' % self.filename
  90.         handle = file(self.filename, 'wb')
  91.         handle.write(content)
  92.         handle.close()
  93.  
  94.     
  95.     def makeFeed(self):
  96.         feed = Feed(self.url)
  97.         self.updateFeed(feed)
  98.         return feed
  99.  
  100.     
  101.     def updateFeed(self, feed):
  102.         feed.update()
  103.         self.processThreads()
  104.         self.processIdles()
  105.  
  106.  
  107.  
  108. class SimpleFeedTestCase(FeedTestCase):
  109.     
  110.     def setUp(self):
  111.         FeedTestCase.setUp(self)
  112.         self.writefile('<?xml version="1.0"?>\n<rss version="2.0">\n   <channel>\n      <title>Liftoff News</title>\n      <link>http://liftoff.msfc.nasa.gov/</link>\n      <description>Liftoff to Space Exploration.</description>\n      <language>en-us</language>\n      <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>\n\n      <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>\n      <docs>http://blogs.law.harvard.edu/tech/rss</docs>\n      <generator>Weblog Editor 2.0</generator>\n      <managingEditor>editor@example.com</managingEditor>\n      <webMaster>webmaster@example.com</webMaster>\n      <item>\n         <title>Star City</title>\n         <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.mov</link>\n         <description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia\'s <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.</description>\n         <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>\n         <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>\n      </item>\n      <item>\n         <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm">partial eclipse of the Sun</a> on Saturday, May 31st.</description>\n         <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>\n         <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>\n      </item>\n      <item>\n         <title>The Engine That Does More</title>\n         <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>\n         <description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly.  The proposed VASIMR engine would do that.</description>\n         <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>\n         <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>\n      </item>\n      <item>\n         <title>Astronauts\' Dirty Laundry</title>\n         <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>\n         <description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them.  Instead, astronauts have other options.</description>\n         <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>\n         <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>\n      </item>\n   </channel>\n</rss>')
  113.  
  114.     
  115.     def testRun(self):
  116.         dialogs.delegate = AcceptScrapeTestDelegate()
  117.         myFeed = self.makeFeed()
  118.         self.assertEqual(dialogs.delegate.calls, 0)
  119.         self.assertEqual(self.everything.len(), 2)
  120.         items = self.everything.filter((lambda x: x.__class__.__name__ == 'Item'))
  121.         self.assertEqual(items.len(), 1)
  122.         myFeed.update()
  123.         self.assertEqual(self.everything.len(), 2)
  124.         items = self.everything.filter((lambda x: x.__class__.__name__ == 'Item'))
  125.         self.assertEqual(items.len(), 1)
  126.         myFeed.remove()
  127.  
  128.  
  129.  
  130. class EnclosureFeedTestCase(FeedTestCase):
  131.     
  132.     def setUp(self):
  133.         FeedTestCase.setUp(self)
  134.         self.writefile('<?xml version="1.0"?>\n<rss version="2.0">\n   <channel>\n      <title>Downhill Battle Pics</title>\n      <link>http://downhillbattle.org/</link>\n      <description>Downhill Battle is a non-profit organization working to support participatory culture and build a fairer music industry.</description>\n      <pubDate>Wed, 16 Mar 2005 12:03:42 EST</pubDate>\n      <item>\n         <title>Bumper Sticker</title>\n         <enclosure url="http://downhillbattle.org/key/gallery/chriscA.mpg" />\n         <description>I\'m a musician and I support filesharing.</description>\n\n      </item>\n      <item>\n         <title>T-shirt</title>\n         <enclosure url="http://downhillbattle.org/key/gallery/payola_tshirt.mpg" />\n      </item>\n      <item>\n         <enclosure url="http://downhillbattle.org/key/gallery/chriscE.mpg" />\n         <description>Flyer in Yucaipa, CA</description>\n      </item>\n      <item>\n         <enclosure url="http://downhillbattle.org/key/gallery/jalabel_nov28.mpg" />\n      </item>\n      <item>\n         <enclosure url="http://downhillbattle.org/key/gallery/jalabel_nov28.jpg" />\n      </item>\n      \n   </channel>\n</rss>')
  135.  
  136.     
  137.     def testRun(self):
  138.         myFeed = self.makeFeed()
  139.         self.assertEqual(self.everything.len(), 5)
  140.         items = self.everything.filter((lambda x: x.__class__.__name__ == 'Item'))
  141.         self.assertEqual(items.len(), 4)
  142.         myFeed.update()
  143.         self.assertEqual(self.everything.len(), 5)
  144.         items = self.everything.filter((lambda x: x.__class__.__name__ == 'Item'))
  145.         self.assertEqual(items.len(), 4)
  146.         for None in items:
  147.             item = None
  148.             self.assertRaises((IndexError,), (lambda : item.entry['enclosures'][1]))
  149.         
  150.         myFeed.remove()
  151.  
  152.  
  153.  
  154. class OldItemExpireTest(FeedTestCase):
  155.     
  156.     def setUp(self):
  157.         FeedTestCase.setUp(self)
  158.         self.counter = 0
  159.         self.writeNewFeed()
  160.         self.feed = self.makeFeed()
  161.         self.items = self.everything.filter((lambda x: x.__class__.__name__ == 'Item'))
  162.         config.set(prefs.TRUNCATE_CHANNEL_AFTER_X_ITEMS, 4)
  163.  
  164.     
  165.     def writeNewFeed(self, entryCount = 2):
  166.         items = []
  167.         for x in range(entryCount):
  168.             self.counter += 1
  169.             items.append('<item>\n <title>Bumper Sticker</title>\n <guid>guid-%s</guid>\n <enclosure url="http://downhillbattle.org/key/gallery/%s.mpg" />\n <description>I\'m a musician and I support filesharing.</description>\n</item>\n' % (self.counter, self.counter))
  170.         
  171.         self.writefile('<?xml version="1.0"?>\n<rss version="2.0">\n   <channel>\n      <title>Downhill Battle Pics</title>\n      <link>http://downhillbattle.org/</link>\n      <description>Downhill Battle is a non-profit organization working to support participatory culture and build a fairer music industry.</description>\n      <pubDate>Wed, 16 Mar 2005 12:03:42 EST</pubDate>\n      %s\n   </channel>\n</rss>' % '\n'.join(items))
  172.  
  173.     
  174.     def checkGuids(self, *ids):
  175.         actual = set()
  176.         for i in self.items:
  177.             actual.add(i.getRSSID())
  178.         
  179.         correct = []([ 'guid-%d' % i for i in ids ])
  180.         self.assertEquals(actual, correct)
  181.  
  182.     
  183.     def parseNewFeed(self, entryCount = 2):
  184.         self.writeNewFeed(entryCount)
  185.         self.updateFeed(self.feed)
  186.  
  187.     
  188.     def testSimpleOverflow(self):
  189.         self.assertEqual(self.items.len(), 2)
  190.         self.parseNewFeed()
  191.         self.assertEqual(self.items.len(), 4)
  192.         self.parseNewFeed()
  193.         self.assertEqual(self.items.len(), 4)
  194.         self.checkGuids(3, 4, 5, 6)
  195.  
  196.     
  197.     def testOverflowWithDownloads(self):
  198.         self.items[0].downloader = FakeDownloader()
  199.         self.items[1].downloader = FakeDownloader()
  200.         self.assertEqual(self.items.len(), 2)
  201.         self.parseNewFeed()
  202.         self.parseNewFeed()
  203.         self.checkGuids(1, 2, 5, 6)
  204.  
  205.     
  206.     def testOverflowStillInFeed(self):
  207.         self.parseNewFeed(6)
  208.         self.checkGuids(3, 4, 5, 6, 7, 8)
  209.  
  210.     
  211.     def testOverflowWithReplacement(self):
  212.         self.counter = 1
  213.         self.parseNewFeed(5)
  214.         self.checkGuids(2, 3, 4, 5, 6)
  215.  
  216.  
  217. if __name__ == '__main__':
  218.     unittest.main()
  219.  
  220.